﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using AGrandVenture;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;

namespace DataEditor
{
    public partial class StatForm : Form
    {
        private List<Stat> stats;
        private Form1 form;
        
        public StatForm(Form1 form)
        {
            InitializeComponent();

            //string[] files = Directory.GetFiles(Application.StartupPath + @"\XML\" + @"\stats\");

            this.form = form;
            stats = new List<Stat>();

            
        }

        private void StatForm_Load(object sender, EventArgs e)
        {
            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;

                foreach (Stat stat in stats)
                {
                    XmlWriter writer = XmlWriter.Create(Application.StartupPath + @"\XML\" + @"\stats\" + stat.Name + ".xml", settings);

                    IntermediateSerializer.Serialize(writer, stat, null);

                    writer.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
                

        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtAbbreviate.Text = "";
            txtDesc.Text = "";

            cboStats.SelectedIndex = -1;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Stat stat = new Stat(txtName.Text);
            stat.Abbreviation = txtAbbreviate.Text;
            stat.Description = txtDesc.Text;

            stats.Add(stat);

            cboStats.Items.Add(stat.Name);

            button2_Click(sender, e);
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = true;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stat stat;

                foreach (string file in dlg.FileNames)
                {
                    stat = GlobalFunctions.LoadStat(file);

                    if (stat != null)
                    {
                        stats.Add(stat);

                        cboStats.Items.Add(stat.Name);
                    }
                }
            }

                
        }


             public static Stat LoadStat(string filename)
        {
            try
            {
                XmlReaderSettings settings = new XmlReaderSettings();

                settings.CloseInput = true;

                XmlReader reader = XmlReader.Create(new FileStream(filename, FileMode.Open), settings);

                Stat stat = IntermediateSerializer.Deserialize<Stat>(reader, null);

                reader.Close();

                return stat;
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
                return null;
            }
        }

             private void cboStats_SelectedIndexChanged(object sender, EventArgs e)
             {
                 if (cboStats.SelectedIndex > -1)
                 {
                     Stat stat = stats[cboStats.SelectedIndex];

                     txtName.Text = stat.Name;
                     txtAbbreviate.Text = stat.Abbreviation;
                     txtDesc.Text = stat.Description;
                 }
                 else
                 {
                     button2_Click(sender, e);
                 }

             }

             private void button1_Click(object sender, EventArgs e)
             {
                 form.hasStats = true;
                 form.gameStats = stats;
                                  
                 this.Close();
             }

             private void button3_Click(object sender, EventArgs e)
             {
                 MessageBox.Show("Fill out Stat Name, Stat Abbreviation and Stat Description, then press Add Stat to add stat to stat list. /n Click New Stat to Add a New Stat. /n Save Stats will save all stats in stat list /n you might need to created the directory /data editor/bin/debug/XML/stats");
             }




    }
}
